package com.cn.wanxi.servlet;

import com.cn.wanxi.IO.CompanyIO;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.NewsService;
import com.cn.wanxi.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/home")//一定有斜杠
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        CompanyModel companyModel = new CompanyModel();//创建实例化对象
        CompanyService service=new CompanyService();
//        CompanyIO companyIO=new CompanyIO();

        req.setAttribute("companyModel", service.getCompanyModel());
//        返回导航数据
        /**
         * 1.新建对象，对象里面属性设置
         * 2.提取数据，因为是六个对象，所以需要用数组装入数据
         * 3.定义一个集合List<NavModel> list = new ArrayList<>();
         * 4.在servlet里面写返回数据 req.setAttribute("navList", navModel.getList());
         * 5.在getList方法里面去遍历数组，将数组里面的数据封装到对象，在将对象加入集合
         * 6.在得到集合以后，我们需要修改jsp页面
         * 7.引入jstl包，在页面上用c:forEach标签
         * <c:forEach items="${navList}" var="navModel">
         *                 <a href="${navModel.href}">
         *                     <div>${navModel.title}</div>
         *                 </a>
         *             </c:forEach>
         *  items裏面的值对应的是servlet里面req.setAttribute("navList"的key值
         *  var 对应的是集合里面的对象
         *  for循环里面的值的取值赋值${navModel.href}
         *  注意：items和循环里面的都需要用${},var不需要
         */
        NavService navService=new NavService();
        req.setAttribute("navList", navService.getList());//key-value的形式

//        得到新闻数据
        NewsService newsService=new NewsService();
        req.setAttribute("newsList", newsService.getList());
/**
 * 1.servlet  HomeService homeservice=new HomeService();
 * req.setAttribute("aa", homeservice.getHomeDto());
 * 2.service   public HomeDto getHomeDto(){
 *     HomeDto homeDto=new HomeDto();
 *     homeDto.setCompnayMode(getCompanyModel());
 *     homeDto.setNavModel(getNavListModel());
 *     return homeDto;
 * }
 * private CompanyModel getCompanyModel(){return null}
 * private List<NavModel> getNavListModel(){return NavModel}
 * 2.HomeDto 里面 company nav  news  product
 */
        //得到产品信息
        ProductService productService=new ProductService();
        req.setAttribute("productList", productService.getList());

        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
    }
}
